/**
 * 智能枚举分析 - 自动发现字段层级关系
 * 无需人工指定，全自动分析
 */

const fs = require('fs');
const Papa = require('papaparse');

const TEST_ROWS = 10000; // 测试用

/**
 * 第一遍：收集基本统计
 */
function collectStats(filePath) {
    return new Promise((resolve, reject) => {
        console.log(`\n📊 第一遍扫描: 收集字段统计信息`);
        
        const fieldStats = {};
        let totalRows = 0;
        let columns = [];
        
        const stream = fs.createReadStream(filePath, { encoding: 'utf-8' });

        Papa.parse(stream, {
            header: true,
            skipEmptyLines: true,
            
            step: function(result, parser) {
                totalRows++;

                if (totalRows === 1 && result.meta.fields) {
                    columns = result.meta.fields;
                    columns.forEach(col => {
                        fieldStats[col] = {
                            values: new Set(),
                            total: 0
                        };
                    });
                }

                columns.forEach(col => {
                    const value = result.data[col];
                    if (!fieldStats[col]) return;
                    
                    fieldStats[col].total++;
                    if (value && value !== '数据不存在') {
                        fieldStats[col].values.add(value);
                    }
                });

                if (totalRows >= TEST_ROWS) {
                    parser.abort();
                }
            },

            complete: function() {
                // 转换Set为数组以便后续处理
                Object.keys(fieldStats).forEach(col => {
                    fieldStats[col].uniqueCount = fieldStats[col].values.size;
                    fieldStats[col].uniqueRatio = fieldStats[col].uniqueCount / totalRows;
                    fieldStats[col].values = Array.from(fieldStats[col].values);
                });
                
                resolve({ totalRows, columns, fieldStats });
            },

            error: reject
        });
    });
}

/**
 * 第二遍：智能发现字段关系
 */
function discoverRelationships(filePath, stats) {
    return new Promise((resolve) => {
        console.log(`\n🔍 第二遍扫描: 智能发现字段关系`);
        
        const { columns, fieldStats } = stats;
        
        // 筛选出可分析的字段（排除唯一性字段）
        const analyzableFields = columns.filter(col => {
            const stat = fieldStats[col];
            const isUnique = stat.uniqueRatio > 0.5 || col.toLowerCase().includes('id');
            return !isUnique;
        });
        
        console.log(`\n可分析字段: ${analyzableFields.join(', ')}`);
        console.log(`尝试发现 ${analyzableFields.length * (analyzableFields.length - 1) / 2} 个可能的字段关系...\n`);
        
        // 尝试所有字段对的组合
        const relationshipCandidates = [];
        for (let i = 0; i < analyzableFields.length - 1; i++) {
            for (let j = i + 1; j < analyzableFields.length; j++) {
                relationshipCandidates.push({
                    field1: analyzableFields[i],
                    field2: analyzableFields[j]
                });
            }
        }
        
        // 分析每对字段的关系
        const relationshipPromises = relationshipCandidates.map(candidate => 
            analyzeFieldPair(filePath, candidate.field1, candidate.field2, stats)
        );
        
        Promise.all(relationshipPromises).then(results => {
            const validRelationships = results.filter(r => r !== null);
            resolve(validRelationships);
        });
    });
}

/**
 * 分析两个字段是否有层级关系
 */
function analyzeFieldPair(filePath, field1, field2, stats) {
    return new Promise((resolve) => {
        const relationMap1to2 = new Map(); // field1 -> field2的映射
        const relationMap2to1 = new Map(); // field2 -> field1的映射
        let rowCount = 0;
        
        const stream = fs.createReadStream(filePath, { encoding: 'utf-8' });
        
        Papa.parse(stream, {
            header: true,
            skipEmptyLines: true,
            step: function(result, parser) {
                rowCount++;
                
                const value1 = result.data[field1];
                const value2 = result.data[field2];
                
                if (value1 && value2 && 
                    value1 !== '数据不存在' && 
                    value2 !== '数据不存在') {
                    
                    // field1 -> field2
                    if (!relationMap1to2.has(value1)) {
                        relationMap1to2.set(value1, new Set());
                    }
                    relationMap1to2.get(value1).add(value2);
                    
                    // field2 -> field1
                    if (!relationMap2to1.has(value2)) {
                        relationMap2to1.set(value2, new Set());
                    }
                    relationMap2to1.get(value2).add(value1);
                }
                
                if (rowCount >= TEST_ROWS) {
                    parser.abort();
                }
            },
            complete: function() {
                // 分析两个方向的关系强度
                const analysis1to2 = analyzeRelationStrength(field1, field2, relationMap1to2);
                const analysis2to1 = analyzeRelationStrength(field2, field1, relationMap2to1);
                
                // 选择关系更强的方向
                let bestRelation = null;
                
                if (analysis1to2.isValid && analysis2to1.isValid) {
                    // 两个方向都有效，选择"一对多"关系更明显的
                    if (analysis1to2.strength > analysis2to1.strength) {
                        bestRelation = analysis1to2;
                    } else {
                        bestRelation = analysis2to1;
                    }
                } else if (analysis1to2.isValid) {
                    bestRelation = analysis1to2;
                } else if (analysis2to1.isValid) {
                    bestRelation = analysis2to1;
                }
                
                if (bestRelation) {
                    console.log(`✅ 发现关系: ${bestRelation.parent} → ${bestRelation.child} (强度: ${bestRelation.strength.toFixed(2)})`);
                }
                
                resolve(bestRelation);
            }
        });
    });
}

/**
 * 分析关系强度
 * 判断标准：
 * 1. 一对多关系：平均每个parent值对应多个child值
 * 2. 层级明显：parent的唯一值数量 < child的唯一值数量
 * 3. 关系稳定：不是完全随机的映射
 */
function analyzeRelationStrength(parentField, childField, relationMap) {
    const parentCount = relationMap.size;
    let totalChildVariety = 0;
    const mappings = {};
    
    relationMap.forEach((childSet, parentValue) => {
        totalChildVariety += childSet.size;
        mappings[parentValue] = Array.from(childSet).sort();
    });
    
    const avgChildPerParent = totalChildVariety / parentCount;
    
    // 判断是否是有效的层级关系
    // 条件1: 平均每个parent至少对应1.5个child（有一定的"扩展性"）
    // 条件2: parent的唯一值数量要少于child（说明是"分类"到"明细"的关系）
    const condition1 = avgChildPerParent >= 1.5;
    const condition2 = parentCount < totalChildVariety;
    
    const isValid = condition1 && condition2;
    
    // 关系强度：平均child数量越多，说明关系越明显
    const strength = avgChildPerParent;
    
    return {
        parent: parentField,
        child: childField,
        parentCount,
        avgChildPerParent: avgChildPerParent.toFixed(2),
        isValid,
        strength,
        mappings
    };
}

/**
 * 生成报告
 */
function generateReport(stats, relationships, outputPath) {
    const { totalRows, columns, fieldStats } = stats;
    
    // 确定跳过和分析的字段
    const analyzableFields = [];
    const skippedFields = [];
    
    columns.forEach(col => {
        const stat = fieldStats[col];
        if (stat.uniqueRatio > 0.5 || col.toLowerCase().includes('id')) {
            skippedFields.push(col);
        } else {
            analyzableFields.push(col);
        }
    });
    
    // 生成字段枚举表格
    let fieldTablesHtml = '';
    analyzableFields.forEach(col => {
        const stat = fieldStats[col];
        
        // 重新扫描获取频率信息
        const valuesWithCount = new Map();
        
        fieldTablesHtml += `
            <div class="field-box">
                <h3>${col}</h3>
                <div class="meta">枚举类型数: <strong>${stat.uniqueCount}</strong> | 总记录: ${stat.total}</div>
                <div class="value-list">
                    ${stat.values.slice(0, 30).map((value, idx) => 
                        `<span class="value-tag">${idx + 1}. ${value}</span>`
                    ).join('')}
                    ${stat.values.length > 30 ? 
                        `<span class="more-tag">... 还有 ${stat.values.length - 30} 个</span>` 
                        : ''}
                </div>
            </div>
        `;
    });
    
    // 生成关系分析
    let relationHtml = '';
    if (relationships.length > 0) {
        relationships.forEach(rel => {
            const sortedMappings = Object.entries(rel.mappings)
                .sort((a, b) => b[1].length - a[1].length);
            
            relationHtml += `
                <div class="relation-box">
                    <h3>${rel.parent} → ${rel.child}</h3>
                    <div class="meta">
                        ${rel.parent}有 <strong>${rel.parentCount}</strong> 种 | 
                        平均每个${rel.parent}包含 <strong>${rel.avgChildPerParent}</strong> 个${rel.child} |
                        关系强度: <strong>${rel.strength.toFixed(2)}</strong>
                    </div>
                    <div class="relation-list">
                        ${sortedMappings.map(([parent, children]) => `
                            <div class="relation-item">
                                <div class="parent">${parent} <span class="badge">${children.length}</span></div>
                                <div class="children">
                                    ${children.map(c => `<span class="tag">${c}</span>`).join('')}
                                </div>
                            </div>
                        `).join('')}
                    </div>
                </div>
            `;
        });
    } else {
        relationHtml = '<p style="color:#999;text-align:center;padding:20px">未发现有意义的字段层级关系</p>';
    }
    
    const html = `<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>智能字段分析</title>
    <style>
        * { margin: 0; padding: 0; box-sizing: border-box; }
        body {
            font-family: "Microsoft YaHei", Arial, sans-serif;
            background: #f5f5f5;
            padding: 20px;
            line-height: 1.6;
        }
        .container {
            max-width: 1200px;
            margin: 0 auto;
            background: white;
            border-radius: 5px;
            box-shadow: 0 2px 8px rgba(0,0,0,0.1);
        }
        .header {
            background: #2c3e50;
            color: white;
            padding: 20px 30px;
            border-radius: 5px 5px 0 0;
        }
        .header h1 { font-size: 1.6em; margin-bottom: 5px; }
        .header .info { opacity: 0.9; font-size: 0.9em; }
        .summary {
            display: flex;
            padding: 15px 30px;
            background: #ecf0f1;
            border-bottom: 1px solid #ddd;
        }
        .summary div { flex: 1; text-align: center; padding: 10px; }
        .summary .label { font-size: 0.9em; color: #666; }
        .summary .value { font-size: 1.6em; font-weight: bold; color: #2c3e50; margin-top: 5px; }
        .content { padding: 30px; }
        h2 {
            color: #2c3e50;
            margin: 25px 0 15px 0;
            padding-bottom: 8px;
            border-bottom: 2px solid #3498db;
        }
        .alert {
            background: #d1ecf1;
            border-left: 4px solid #0c5460;
            padding: 15px 20px;
            margin-bottom: 25px;
            border-radius: 4px;
            color: #0c5460;
        }
        .skipped {
            background: #fff3cd;
            padding: 12px 15px;
            border-radius: 4px;
            margin-bottom: 25px;
            border-left: 3px solid #ffc107;
        }
        .skipped ul { list-style: none; padding-left: 10px; }
        .skipped li { padding: 3px 0; color: #856404; }
        .field-box, .relation-box {
            margin-bottom: 25px;
            border: 1px solid #ddd;
            border-radius: 5px;
            padding: 20px;
            background: #fafafa;
        }
        .field-box h3, .relation-box h3 {
            color: #2c3e50;
            font-size: 1.2em;
            margin-bottom: 10px;
        }
        .meta {
            color: #666;
            margin-bottom: 15px;
            padding: 8px;
            background: white;
            border-radius: 3px;
            border-left: 3px solid #3498db;
        }
        .meta strong { color: #3498db; }
        .value-list {
            display: flex;
            flex-wrap: wrap;
            gap: 8px;
        }
        .value-tag {
            background: white;
            padding: 6px 12px;
            border-radius: 4px;
            font-size: 0.9em;
            border: 1px solid #ddd;
        }
        .more-tag {
            background: #3498db;
            color: white;
            padding: 6px 12px;
            border-radius: 4px;
            font-size: 0.9em;
        }
        .relation-list {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(350px, 1fr));
            gap: 15px;
        }
        .relation-item {
            border: 1px solid #ddd;
            border-radius: 4px;
            padding: 12px;
            background: white;
        }
        .parent {
            font-weight: bold;
            color: #2c3e50;
            margin-bottom: 8px;
            padding-bottom: 8px;
            border-bottom: 1px solid #ddd;
        }
        .badge {
            float: right;
            background: #3498db;
            color: white;
            padding: 2px 8px;
            border-radius: 10px;
            font-size: 0.85em;
        }
        .children {
            display: flex;
            flex-wrap: wrap;
            gap: 6px;
        }
        .tag {
            background: #ecf0f1;
            padding: 4px 8px;
            border-radius: 3px;
            font-size: 0.85em;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1>🤖 智能字段枚举分析</h1>
            <div class="info">自动发现字段关系 · 测试${totalRows}行 · ${new Date().toLocaleString('zh-CN')}</div>
        </div>

        <div class="summary">
            <div>
                <div class="label">分析行数</div>
                <div class="value">${totalRows}</div>
            </div>
            <div>
                <div class="label">总字段</div>
                <div class="value">${columns.length}</div>
            </div>
            <div>
                <div class="label">分析字段</div>
                <div class="value">${analyzableFields.length}</div>
            </div>
            <div>
                <div class="label">发现关系</div>
                <div class="value">${relationships.length}</div>
            </div>
        </div>

        <div class="content">
            <div class="alert">
                <strong>💡 智能分析说明</strong><br>
                本报告通过算法自动发现字段间的层级关系，无需人工指定。<br>
                判断标准：1) 一对多关系 2) 父字段唯一值少于子字段 3) 平均关联度 ≥ 1.5
            </div>

            ${skippedFields.length > 0 ? `
                <div class="skipped">
                    <strong>⏭️ 自动跳过的字段（唯一性字段）：</strong>
                    <ul>${skippedFields.map(f => `<li>• ${f}</li>`).join('')}</ul>
                </div>
            ` : ''}

            <h2>🔗 自动发现的字段关系</h2>
            ${relationHtml}

            <h2>📋 字段枚举详情</h2>
            ${fieldTablesHtml}
        </div>
    </div>
</body>
</html>`;

    fs.writeFileSync(outputPath, html, 'utf-8');
}

/**
 * 主函数
 */
async function main() {
    const filePath = 'output/基本设施.csv';
    const outputPath = 'output/智能分析报告.html';

    console.log('='.repeat(60));
    console.log('🤖 智能字段枚举分析 - 自动发现关系');
    console.log('='.repeat(60));

    try {
        // 第一遍：收集统计
        const stats = await collectStats(filePath);
        
        // 第二遍：发现关系
        const relationships = await discoverRelationships(filePath, stats);
        
        console.log(`\n✅ 共发现 ${relationships.length} 个有意义的字段关系`);
        
        // 生成报告
        console.log('\n生成报告...');
        generateReport(stats, relationships, outputPath);
        
        console.log(`\n✅ 报告已生成: ${outputPath}`);
        console.log('双击打开查看智能分析结果！\n');
        
    } catch (error) {
        console.error('错误:', error);
    }
}

main();

